<?php


namespace App\Http\Controllers\Estate;


use App\Http\Controllers\Controller;
use App\Models\FleaOrder;
use Illuminate\Http\Request;

class FleaOrderController extends Controller
{
    public function index(Request $request)
    {
        $where = [];
        $state = $request->get('state');
        if ($state) {
            $where[] = ['state', '=', $state];
        }
        $user_id = $request->get('user_id');
        if ($user_id) {
            $where[] = ['user_id', '=', $user_id];
        }

        $estate_user = getUserIds();
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);

        $offset = ($page == 1) ? 0 : ($page - 1) * $limit;
        $data = FleaOrder::query()->with('goods:id,goods_name,goods_images')->where($where)->whereIn('user_id', $estate_user)->offset($offset)->limit($limit)->orderByDesc('id')->get();

        $response['data'] = $data;
        $response['count'] = FleaOrder::query()->where($where)->count();
        $response['pages'] = ceil($response['count'] / $limit);

        return $this->success($response);
    }

    public function show($id)
    {
        $estate_user = getUserIds();
        $data = FleaOrder::query()->where('id', $id)->whereIn('user_id', $estate_user)->first();
        $data->goods;
        $data->user;
        $data->comment;
        return $this->success($data);
    }
}
